/*测试数据*/
var testdatabrand=[
	{
		id: 1,
		brandName: 'linda',
		createTime: 1502937009
	},
	{
		id: 1,
		brandName: 'dadd',
		createTime: 1502937009
	},
	{
		id: 1,
		brandName: 'ds',
		createTime: 1502937009
	},
	{
		id: 1,
		brandName: 'ssfdd',
		createTime: 1502937009
	}
];
var testdata={
		id: 1,
		productID: 2322,
		productName: "18817802281",
		productBrand: 'linda',
		productVersion: 'V3高规',
		store: 3455,
		singlePrice: 234,
		tradeCnt: 2234,
		createTime: 1502937009
	};

$(function(){
	getBrandMes();
	$("#btnSaveEdit").on('click',function(){
		submitData();
	});
	$("#btnReback").on('click',function(){
		var tgid=getUrlParam("tgid");
		if(tgid!=null && tgid!=""){
			history.go(-1);
		}else{
			$("#editForm")[0].reset();
		}
	});
});
/*获取品牌数据*/
function getBrandMes(){
	/*var datarows=testdatabrand;
	if(datarows.length>0){
		$("#productBrand").empty();
		$.each(datarows, function(i,item) {
			var $option=$("<option>").val(item.brandName).text(item.brandName);
			$("#productBrand").append($option);
		});
		getDataMes();
	}else{
		consoleMes($("#manageContent"),3000,"warning","无相关品牌数据，请先添加品牌！");//success、info、warning、danger
		setTimeout(function () {
	    	window.location.href=gobalWeb+"product/brandmg.html";
	    }, 2500);
	}*/
    $.post(
        gobalServer+"productBrand/selectAll",
        {},
        function(backData){
            if(backData.statusCode==200){
                var datarows=backData.commonList;
                if(datarows.length>0){
                    $("#productBrand").empty();
                    $.each(datarows, function(i,item) {
                        var $option=$("<option>").val(item.brandName).text(item.brandName);
                        $("#productBrand").append($option);
                    });
                    getDataMes();
                }else{
                    consoleMes($("#manageContent"),3000,"warning","无相关品牌数据，请先添加品牌！");//success、info、warning、danger
                    setTimeout(function () {
                        window.location.href=gobalWeb+"product/brandmg.html";
                    }, 2500);
                }
            }else{
                consoleMes($("#manageContent"),3000,"warning",backData.msg);//success、info、warning、danger
            }
        }
    );
};
/*修改数据时，获取数据并显示*/
function getDataMes(){
	var tgid=getUrlParam("tgid");
	if(tgid!=null && tgid!=""){
		/*
		if(testdata.id!=null){
			$("#manageContent").attr({"tgid":testdata.id});
		}
		if(testdata.productName!=null){
			$("#productName").val(testdata.productName);
		}
		if(testdata.productBrand!=null){
			$("#productBrand").val(testdata.productBrand);
		}
		if(testdata.productVersion!=null){
			$("#productVersion").val(testdata.productVersion);
		}
		if(testdata.store!=null){
			$("#store").val(testdata.store);
		}
		if(testdata.singlePrice!=null){
			$("#singlePrice").val(testdata.singlePrice);
		}*/
        $.post(
            gobalServer+"product/selectOne",
            {"productId":tgid},
            function(backData){
                if(backData.statusCode==200){
                    var datarow=backData.object;
                    if(datarow.productId!=null){
                        $("#manageContent").attr({"tgid":datarow.productId});
                    }
                    if(datarow.productName!=null){
                        $("#productName").val(datarow.productName);
                    }
                    if(datarow.productBrand!=null){
                        $("#productBrand").val(datarow.productBrand);
                    }
                    if(datarow.productVersion!=null){
                        $("#productVersion").val(datarow.productVersion);
                    }
                    if(datarow.store!=null){
                        $("#store").val(datarow.store);
                    }
                    if(datarow.singlePrice!=null){
                        $("#singlePrice").val(datarow.singlePrice);
                    }
                }else{
                    consoleMes($("#manageContent"),3000,"warning",backData.msg);//success、info、warning、danger
                }
            }
        );
	}
};
/*新增或修改数据*/
function submitData(){
	var tgid=$("#manageContent").attr("tgid");
	var productName=$("#productName").val().trim();
	var productBrand=$("#productBrand").val().trim();
	var productVersion=$("#productVersion").val().trim();
	var store=0;
	var singlePrice=$("#singlePrice").val().trim();
	if(productName==null || productName==""){
		consoleMes($("#manageContent"),2000,"warning","产品名称不能为空！");
		return;
	}
	if(productBrand==null || productBrand==""){
		consoleMes($("#manageContent"),2000,"warning","产品品牌不能为空！");
		return;
	}
	// if(store==null || store<1){
	// 	consoleMes($("#manageContent"),2000,"warning","库存不能为空，且最小1件！");
	// 	return;
	// }
	if(singlePrice==null || singlePrice<=0){
		consoleMes($("#manageContent"),2000,"warning","产品单价不能为空，且大于0元！");
		return;
	}
	if(tgid!=null && tgid!=""){
		if(getUrlParam("tgid")!=null){
			/*consoleMes($("#manageContent"),2000,"success","数据修改成功，正在刷新！");//success、info、warning、danger
        	setTimeout(function () {
		    	window.location.reload();
		    }, 2500);*/
            $.post(
                gobalServer+"product/update",
                {"productId":tgid,"productName":productName,
                    "productBrand":productBrand,"productVersion":productVersion,
                    "store":0,"singlePrice":singlePrice},
                function(backData){
                    if(backData.statusCode==200){
                        consoleMes($("#manageContent"),2000,"success","数据修改成功，正在刷新！");//success、info、warning、danger
                        setTimeout(function () {
                            window.location.reload();
                        }, 2500);
                    }else{
                        consoleMes($("#manageContent"),3000,"warning",backData.msg);//success、info、warning、danger
                    }
                }
            );

	    }else{
			consoleMes($("#manageContent"),2000,"warning","无法提交，请刷新页面重试！");
		}
	}else{
		/*consoleMes($("#manageContent"),2000,"success","添加数据成功，正在跳转！");//success、info、warning、danger
    	setTimeout(function () {
	    	window.location.href=gobalWeb+"product/productshow.html";
	    }, 2500);*/
        $.post(
            gobalServer+"product/insert",
            {"productName":productName,
                "productBrand":productBrand,"productVersion":productVersion,
                "store":store,"singlePrice":singlePrice},
            function(backData){
                if(backData.statusCode==200){
                    consoleMes($("#manageContent"),2000,"success","添加数据成功，正在跳转！");//success、info、warning、danger
                    setTimeout(function () {
                        window.location.href=gobalWeb+"product/productshow.html";
                    }, 2500);
                }else{
                    consoleMes($("#manageContent"),3000,"warning",backData.msg);//success、info、warning、danger
                }
            }
        );

	}
		
};
